//
//  LogonViewController.m
//  JiJinFinance
//
//  Created by 李东晓 on 15/12/10.
//  Copyright © 2015年 LiDongXiao. All rights reserved.
//

#import "LogonViewController.h"
#import "LogonVe.h"
#import "HttpTool.h"
#import "LogonModel.h"
//#import "TSMessageView.h"

#import <tsmessages/TSMessageView.h>

@interface LogonViewController ()<MyDelegate>
{
    NSInteger _timerNo;
    NSTimer * _timer;
}
@property (nonatomic ,strong)NSMutableArray * dataArray;
@property (nonatomic ,strong)LogonVe * lgView;
@end

@implementation LogonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"注册";
    self.dataArray = [[NSMutableArray alloc]init];
    
    _timerNo = 60;
    
    NSArray * arr = [[NSBundle mainBundle]loadNibNamed:@"LogonVe" owner:self options:nil];
    self.lgView = arr.lastObject;
    
    self.lgView.delegate = self;
    
    
//    lgView.RedYes.adjustsFontSizeToFitWidth = YES;
//    lgView.Member.titleLabel.adjustsFontSizeToFitWidth =  YES;
    
    [self.lgView.ButAuthCode setTitle:@"(60)重新验证" forState:UIControlStateDisabled];
    
    [self.lgView setFrame:CGRectMake(0, 0, kUIScreenWidth, self.lgView.frame.size.height)];
    
    UIScrollView * scr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight)];
    
    [scr setContentSize:CGSizeMake(0, 450)];
    
    self.view = scr;
    
    [scr addSubview:self.lgView];
    
}


#pragma mark-注册按钮的点击方法
-(void)registerDelegateClick
{
    NSLog(@"注册");
}
#pragma mark-判断点击注册时各项数据是否一致
-(void)registerJudgement
{
    if (self.lgView.FiledCode != self.lgView.FiledCode) {
        [TSMessage showNotificationInViewController:self title:@"温馨提示" subtitle:@"请确认您再次输入的密码" type:TSMessageNotificationTypeWarning duration:2];
    }
    
    
}
#pragma mark-验证码的点击方法
-(void)gainCodeDelegateClick
{
    //验证码测试
    [self testAuthCode];
    NSLog(@"验证码");
}


#pragma mark-验证码测试
-(void)testAuthCode
{
    //如果textfiled里没有内容将无法请求数据会有添加textfiled的提示
    if ([self.lgView.FiledResigter.text isEqualToString:@""]) {
        [TSMessage showNotificationInViewController:self title:@"温馨提示" subtitle:@"请输入您的手机号" type:TSMessageNotificationTypeError duration:2.0];
    }
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    else
    {
    NSString *netPath = @"/regist/regPhoneCode";
//    NSDictionary *params = [NSDictionary dictionaryWithObject: self.lgView.FiledResigter forKey:@"phone"];
    NSDictionary *params = [NSDictionary dictionaryWithObject: self.lgView.FiledResigter.text forKey:@"phone"];
    NSLog(@"<><><><><><>%@",self.lgView.FiledResigter.text);

    [HttpTool postWithPath:netPath params:params success:^(id responseObj) {
    
        NSLog(@"++++++++%@",responseObj);
        //验证已发送
        //        NSString * returnInfo = [responseObj objectForKey:@"returnInfo"];
        NSDictionary * content = [responseObj objectForKey:@"content"];
   
        //第一次点击验证码的时候返回的内容
        NSString * returnNo = [responseObj objectForKey:@"returnNo"];
       
        NSString * returnInfo = [responseObj objectForKey:@"returnInfo"];
        NSLog(@"%@",returnInfo);
        NSLog(@"%@",returnNo);
        if ([returnNo isEqualToString:@"0000"]) {
            NSString * auth_code = [content objectForKey:@"auth_code"];
            NSLog(@"验证码%@",auth_code);
            _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:_timer repeats:YES];
        }else
        {
            NSLog(@"点击不了");
            [TSMessage showNotificationInViewController:self title:@"温馨提示" subtitle:@"请输入正确的手机号" type:TSMessageNotificationTypeWarning duration:2.0];
        }
        
    } failure:^(NSError *error) {
        
    }];
    
    }
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //else里面的内容
}
#pragma mark- 验证码倒计时
-(void)timerAction:(NSTimer * )timer
{
    _timerNo--;
   
    self.lgView.ButAuthCode.enabled = YES;
    [self.lgView.ButAuthCode setTitle:[NSString stringWithFormat:@"(%zd)重新验证",_timerNo] forState:UIControlStateNormal];
    NSLog(@"%@",self.lgView.ButAuthCode.titleLabel.text);
    //用来判断是否验证码倒计时结束
    if (_timerNo == 0) {
        _timerNo = 60;
        [_timer invalidate];
        [self.lgView.ButAuthCode setTitle:@"(60)重新验证" forState:UIControlStateNormal];
    }
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
